package com.offcn.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayAccount;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.offcn.pay.service.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AliPayServiceImpl implements AliPayService {

    @Autowired
    private AlipayClient alipayClient;

    @Value("${alipay.notifyUrl}")
    private String notifyUrl;

    @Value("${mq.exchange}")
    private String exchange;

    @Value("${mq.routingKey}")
    private String routingKey;

    @Override
    public Map createQrCode(String out_trade_no, String total_amount) {
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", out_trade_no);
        bizContent.put("total_amount", total_amount);
        bizContent.put("subject", "dongyimai");
        request.setBizContent(bizContent.toString());
        try {
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            System.out.println("预下单返回结果:"+response.isSuccess()+" body:"+response.getBody());
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("out_trade_no",response.getOutTradeNo());
            resultMap.put("total_amount",total_amount);
            resultMap.put("qr_code",response.getQrCode());
            return resultMap;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Map<String,String> queryPayStatus(String out_trade_no) {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{ \"out_trade_no\":\"" + out_trade_no + "\"}");

        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            System.out.println("--------------------------");
            System.out.println(response);
            System.out.println("--------------------------");
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("out_trade_no",response.getOutTradeNo());
            resultMap.put("trade_no",response.getTradeNo());
            resultMap.put("status",response.getTradeStatus());
            return resultMap;
        } catch(Exception ex) {
            System.out.println(ex);
            return null;
        }
    }

    @Override
    public Map createSeckillQrCode(Map<String, String> paramters) {
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl(notifyUrl); // 回调方法的设定
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", paramters.get("out_trade_no"));
        bizContent.put("total_amount", paramters.get("total_amount"));
        // 设定为 消息中间件的交换机，路由，消息
        bizContent.put("body","exchange=" + exchange
                                + "&routingKey=" + routingKey
                                + "&username=" + paramters.get("username")
        );
        bizContent.put("subject", "dongyimai");
        request.setBizContent(bizContent.toString());
        try {
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("out_trade_no",response.getOutTradeNo());
            resultMap.put("total_amount",paramters.get("total_amount"));
            resultMap.put("qr_code",response.getQrCode());
            return resultMap;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return null;
        }
    }
}
